package cc.orange.utils.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cc.orange.R;
import cc.orange.adapter.HomePopupAdapter;

/**
 */

public class PopupDiscoView {

    private static View inflate;
    private static HomePopupAdapter adapter;
    private static PopupWindow popupWindow;
    public static void showPopupwindow(final Context context,
                                       View view,PopuStatus pp) {//回调

        if (popupWindow == null && inflate == null) {
            inflate = LayoutInflater.from(context).inflate(R.layout.item_discover_popup, null);
            popupWindow = new PopupWindow(inflate, RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
        }
        //设置背景,这个没什么效果，不添加会报错
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //设置点击弹窗外隐藏自身
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);

        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        //设置位
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
//        backgroundAlpha(0.3f, (Activity) context);

//        popupWindow.showAsDropDown(view);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
//                backgroundAlpha(1f,(Activity) context);

            }
        });


        TextView item_dispopu_text1 = inflate.findViewById(R.id.item_dispopu_text1);
        TextView item_dispopu_text2 = inflate.findViewById(R.id.item_dispopu_text2);

        item_dispopu_text1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                pp.popupDismissCancle("1");
            }
        });

        item_dispopu_text2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                pp.popupDismissCancle("2");
            }
        });
        pp.popupShow();
    }

    public static void backgroundAlpha(float bgAlpha, Activity context) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        context.getWindow().setAttributes(lp);
    }


    public interface PopuStatus {
        void popupShow();
        void popupDismissCancle(String se);

    }
}
